<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //参数名相同，参数的类型不同，参数的个数不同
      function add() {
        if (arguments.length < 2) {
          throw new Error('参数不能小于2个')
        }

        let result = 0
        for (let i = 0; i < arguments.length; i++) {
          result += arguments[i]
        }

        return result
      }

      console.log(add(1, 2))
      console.log(add(1, 2, 3))
      console.log(add(1, 2, 3, 4))

      //利用剩余参数实现重载
      function add2(...params) {
        if (params.length < 2) {
          throw new Error('参数不能小于两个')
        }

        let result = 0
        for (let i = 0; i < params.length; i++) {
          result += params[i]
        }

        return result
      }

      console.log(add2(1, 2))
      console.log(add2(1, 2, 3))
      console.log(add2(1, 2, 3, 4))
    </script>
  </body>
</html>
